package com.leyou.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.config.OSSProperties;
import com.leyou.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.*;

@Service
public class UploadServiceImpl implements UploadService {


    // 支持的文件类型
    private static final List<String> suffixes = Arrays.asList("image/png", "image/jpeg", "image/bmp");


    @Override
    public String uploadImage(MultipartFile file) {
//         // 1、图片信息校验
//        // 1)校验文件类型
        String contentType = file.getContentType();
        if (!suffixes.contains(contentType)){
//            没有抛异常
            throw  new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }



        // 2)校验图片内容
        BufferedImage image = null;
        try {
            image = ImageIO.read(file.getInputStream());
        } catch (IOException e) {
            throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }
        if (image == null){
            throw new LyException(ExceptionEnum.INVALID_FILE_TYPE);
        }

        // 2、保存图片
        // 2.1、生成保存目录,保存到nginx所在目录的html下
        //      这样可以直接通过nginx来访问到
        File dir = new File("D:/stu/nginx-1.12.2/html");
        if (!dir.exists()){
//            不存在创建
            dir.mkdirs();
        }
//        保存图片
        try {
            file.transferTo(new File(dir,file.getOriginalFilename()));
//            拼接图片地址
            return "http://image.leyou.com/" + file.getOriginalFilename();
        } catch (Exception e) {
            throw new LyException(ExceptionEnum.FILE_UPLOAD_ERROR);
        }

    }

//   图片上传到阿里云

        @Autowired
        private OSSProperties prop;

        @Autowired
        private OSS client;

        // ...

        public Map<String, Object> getSignature() {
            try {
                long expireTime = prop.getExpireTime();
                long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
                Date expiration = new Date(expireEndTime);
                PolicyConditions policyConds = new PolicyConditions();
                policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, prop.getMaxFileSize());
                policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, prop.getDir());

                String postPolicy = client.generatePostPolicy(expiration, policyConds);
                byte[] binaryData = postPolicy.getBytes("utf-8");
                String encodedPolicy = BinaryUtil.toBase64String(binaryData);
                String postSignature = client.calculatePostSignature(postPolicy);

                Map<String, Object> respMap = new LinkedHashMap<>();
                respMap.put("accessId", prop.getAccessKeyId());
                respMap.put("policy", encodedPolicy);
                respMap.put("signature", postSignature);
                respMap.put("dir", prop.getDir());
                respMap.put("host", prop.getHost());
                respMap.put("expire", expireEndTime);
                return respMap;
            }catch (Exception e){
                throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
            }
        }
}
